const tabBarConfig = require("./config")

const app = getApp<IAppOption>()
Component({  
  properties: {}, // 如果有属性，可以在这里定义  

  data: {  
    selected: '',  
    isProtrude: false,  
    color: "#7A7E83",  
    selectedColor: "#3E7AE9",  
    list: [] as any[], // 这里你可能需要定义更具体的类型，基于 tabBarConfig 的内容  
  },  
  
  attached() {  
    const role = app.globalData.role;  
    this.changeTabBar(role); 
  },  
  
  methods: {  
    switchTab(e: any) {  
      const data = e.currentTarget.dataset;  
      const url = data.path;  
      const protrude = data.protrude;  
      if (protrude) {  
        wx.navigateTo({ url });  
      } else {  
        wx.switchTab({ url });  
      }  
      this.setData({  
        selected: url,  
      });  
    },  
  
    changeTabBar(roles: Role[]) {  
      const roleGrad = ['boss', 'admin', 'collector', 'planner', 'client'];  

      const { toDoListBar, personnelBar, addBar, warehouseBar, userBar, company} = tabBarConfig;
      const tabBarList: Record<string, any[]> = {  
        'boss': [toDoListBar, personnelBar, addBar, warehouseBar, company],
        'admin': [toDoListBar, personnelBar, addBar, warehouseBar, userBar],
        'collector': [toDoListBar, addBar, userBar],
        'planner': [toDoListBar, addBar, userBar],
        'client': [toDoListBar, userBar],
      };  
  
      const maxRole = this.getMaxRole(roles, roleGrad);  
      this.setData({  
        list: tabBarList[maxRole],  
        isProtrude: Boolean(tabBarList[maxRole].find(item => item.protrude)),  
      });  
    },  
  
    getMaxRole(roles: string[], roleGrad: string[]): string {  
      let maxRole: string | undefined;  
      for (const role of roles) {  
        if (roleGrad.includes(role) && (!maxRole || roleGrad.indexOf(role) < roleGrad.indexOf(maxRole))) {  
          maxRole = role;  
        }  
      }  
      if (!maxRole) {  
        maxRole = roleGrad[roleGrad.length - 1];  
      }  
      return maxRole;  
    },  
  },  
});